from querier.esquerier import ElasticSearchQuerier
# from querier.article_search.article_search import ArticleSearchQuerier
import utils.simhash as sh


class ArticleOriginalityCheckQuerier(ElasticSearchQuerier):
    def __init__(self, article_search):
        super(ArticleOriginalityCheckQuerier, self).__init__(None, None, None)
        self.article_search = article_search

    def _build_query(self, args):
        pass

    def _build_result(self, es_result, param):
        pass

    def search(self, args):
        """
        search title and text and returns title and text similarity
        :param args:
        :return:
        """

        title = args.get('title')
        text = args.get('text')
        filters = args.get('filters')
        from_ = args.get('from', 0)
        size = args.get('size', 10)
        from_ = 0 if not from_ else from_
        size = 100 if not size else size

        title_sim = []
        text_sim = []

        if title:
            # title search
            title_args = {
                'term': title,
                'exact': True,
                'deduplicate': False,
                'filters': filters,
                'from': from_,
                'size': size,
                'order_by': 'relative'

            }
            res = self.article_search.search(title_args)
            for a in res.get('articles', []):
                url = a.get('url', '')
                t = a.get('title')
                s = sh.string_similar(title, t)
                title_sh = a.get('title_simhash')
                tsh = sh.encode_simhash(sh.get_hash(sh.tokenize(t), 64))
                tsh2 = sh.get_hash(sh.tokenize(t), 64)
                t2 = sh.decode_simhash(title_sh)[0]
                s2 = sh.similarity(t2, tsh2)
                title_sim.append({'title': t, 'similarity': s, 'url': url, 'kw': a.get('keywords'), 'from': a.get('from')})

        if text:
            text_args = {
                'term': text,
                'exact': False,
                'deduplicate': False,
                'filters': filters,
                'from': from_,
                'size': size,
                'order_by': 'relative'
            }

            # text search
            res = self.article_search.search(text_args)
            for a in res.get('articles', []):
                t = a.get('title')
                url = a.get('url')
                # kw = a.get('raw_keywords')
                abstract = a.get('abstract')

                text_simhash_b64 = a.get('text_simhash')
                tokens = sh.tokenize(text)
                # s = sh.string_similar(abstract.strip(), text.strip()[0:51])
                print('title: ', t, 'abstract: ', abstract, 'text: ', text.strip())
                ts = sh.get_hash(tokens, 64)
                hash_tmp = sh.encode_simhash(ts)
                text_simhash = sh.decode_simhash(text_simhash_b64)[0]
                s = sh.similarity(ts, text_simhash)
                text_sim.append({'title': t, 'similarity': s, 'url': url, 'from': a.get('from'), 'simhash': text_simhash, 'sb64': text_simhash_b64, 'tmp': hash_tmp})
        title_sim = sorted(title_sim, key=lambda x: x.get('similarity'), reverse=True)[0:100]
        text_sim = sorted(text_sim, key=lambda x: x.get('similarity'), reverse=True)[0:100]
        title_originality = 1 - title_sim[0].get('similarity', 0)
        text_originality = 1 - text_sim[0].get('similarity', 0)
        return {
            'similarity': {
                'title_originality': title_originality,
                'text_originality': text_originality,
                'title_sim': title_sim,
                'text_sim': text_sim
            }
        }

